package edu.wpi.cs509.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import edu.wpi.cs509.representation.StrategoPiece;
import edu.wpi.cs509.representation.TeamColor;
import edu.wpi.cs509.ui.images.StrategoPieceImage;


/**
 * A GraveyardPlot is the panel that displays the Stratego piece
 * graphic image, and the count of those pieces (the headstone).
 * 
 * @author afoltan
 *
 */
@SuppressWarnings("serial")
public class GraveyardPlot extends JPanel {
	
	// the piece shown in this plot
	private StrategoPiece piece;
	
	// the count of this type of piece
	private int count;
	
	// the graphic label showing the count
	private JLabel headstone;
	
	// the graphic container for drawing the image
	private GraveyardGrave grave;
	
	// the background color for the plot
	private Color bgColor = Color.ORANGE;
	
	// an indication of whether this plot has been selected by the mouse
	private boolean selected = false;
	
	// the graeyard this plot belongs to
	private Graveyard graveyard;
	
	//the mouse event listener for this plot
	private GraveyardMouseListener mouseListener;
			
	/**
	 * Constructor for the GraveyardPlot
	 * @param yard - the graveyard this plot belongs to
	 * @param piece - the stratego piece this plot represents
	 * @param count - the initial count of pieces to display
	 * @param notify - the notification callback for graveyard events
	 */
	public GraveyardPlot( Graveyard graveyard, StrategoPiece piece, int count, GraveyardNotify notify ) {
		
		this.piece = piece;
		this.graveyard = graveyard;
		
		// define the plot layout (vertical)
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		// create the grave (image)
		StrategoPieceImage image = StrategoPieceImage.get(piece);
		grave = new GraveyardGrave( image, graveyard.getColor() );
		add( grave );
		grave.setVisible(true);
		
		// create the headstone (count of pieces)
		headstone = new JLabel( "000" );
		headstone.setHorizontalAlignment(JTextField.CENTER);
		
		// fix the text object size
		Dimension d = headstone.getPreferredSize();
		headstone.setMinimumSize( d );
		headstone.setMaximumSize( d );
		add( headstone );
		
		// set the initial count
		setCount( count );
		headstone.setVisible(true);
		
		// create the mouse event listener
		if (notify != null) {
			mouseListener = new GraveyardMouseListener( this, notify );
			addMouseListener( mouseListener ) ;
		}

        // show it
		setVisible(true);
	}
	
	/**
	 * Retrieves the stratego piece associated with this plot
	 * @return - the StrategoPiece
	 */
	public StrategoPiece getPiece() {
		return piece;
	}
	
	/**
	 * Gets the count of pieces available
	 * @return - the count
	 */
	public int getCount() {
		return count;
	}
	
	/**
	 * Sets the count of pieces available
	 * @param count - the new count
	 */
	public void setCount( int count ) {
		this.count = count;
		headstone.setText( String.valueOf(count) );
	}
	
	/**
	 * Redraws the GraveyardPlot container
	 * @param g - the graphics object used to redraw this container
	 */
    public void paintComponent(Graphics g) {
    	
		Insets inset = getInsets();
		
		int w = getWidth() - inset.left - inset.right;
		int h = getHeight() - inset.top - inset.bottom;
		
		g.setColor( bgColor );
  		g.fillRect( inset.left, inset.top, w, h );
				
	}

    /**
     * Retrieves the GraveyardGrave panel of this plot
     * @return - the GraveyardGrave
     */
	public GraveyardGrave getGraveyardGrave() {
		return grave;
	}
	
	/**
	 * Sets the 'selected' state of this plot.  When selected,
	 * the plot color changes to highlight the plot.
	 * @param val -
	 * 		true  : selected
	 * 		false : not selected
	 */
	public void setSelected( boolean val ) {
		selected = val;
		if (selected) {
			bgColor = bgColor.brighter();
		} else {
			bgColor = Color.ORANGE;
		}
		repaint();
	}

	/**
	 * Indicates whether thie plot is currently selected.
	 * @return - 
	 * 		true  : this plot is selected
	 * 		false : this plot is not selected 
	 */
	public boolean isSelected() {
		return selected;
	}

	/**
	 * Indicates the player color of this plot
	 * @return - the player color (RED or BLUE)
	 */
	public TeamColor getPlayerColor() {
		return graveyard.getColor();
	}

	/**
	 * Retrieves the StrategoPiece represented by this plot
	 * @return - the StrategoPiece
	 */
	public StrategoPiece getStrategoPiece() {
		return piece;
	}

	/** 
	 * Retrieves the graveyard this plot belongs to
	 * @return - the Graveyard
	 */
	public Graveyard getGraveyard() {
		return graveyard;
	}
	
}
